<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form
          class="demo-form-inline"
          :label-position="labelPosition"
          label-width="80px"
        >
          <el-row>
            <el-col class="preface">
              <span>说明：目前支持学科和关键字条件筛选</span>
              <el-button
                type="success"
                size="small"
                icon="el-icon-edit"
                @click="Newadditions"
                >新增试题</el-button
              >
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select
                v-model="b1t.subjectID"
                  placeholder="请选择"
                  size="small"
                  style="width: 233px"
                  @change="changeSelect"
                >
                <el-option
                    v-for="item in subject"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录">
                <el-select
                  placeholder="请选择"
                  size="small"
                  style="width: 233px"
                  v-model="b1t.catalogID"
                >
                <el-option
                    v-for="item in directorys"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签">
                <el-select
                  placeholder="请选择"
                  size="small"
                  style="width: 233px"
                  v-model="b1t.tags"
                >
                  <el-option
                  v-for="item in labelTags"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字">
                <el-input
                  placeholder="根据题干搜索"
                  size="small"
                  style="width: 233px"
                  v-model="b1t.keyWord"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select
                  placeholder="请选择"
                  size="small"
                  style="width: 233px"
                  v-model="b1t.questionType"
                >
                  <el-option
                  v-for="item in questionType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度">
                <el-select
                  placeholder="请选择"
                  size="small"
                  style="width: 233px"
                  v-model="b1t.difficulty"
                >
                  <el-option
                  v-for="item in difficulty"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向">
                <el-select
                  placeholder="请选择"
                  size="small"
                  style="width: 233px"
                  v-model="b1t.direction"
                >
                  <el-option
                  v-for="item in direction"
                  :key="item"
                  :label="item"
                  :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select
                  placeholder="请选择"
                  size="small"
                  style="width: 233px"
                  v-model="b1t.creatorID"
                >
                  <el-option
                  v-for="item in userInfor"
                  :key="item.id"
                  :label="item.username"
                  :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input
                  size="small"
                  style="width: 233px"
                  v-model="b1t.remarks"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input
                  size="small"
                  style="width: 233px"
                  v-model="b1t.shortName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="supplier" label="城市" style="width:100%">
                <el-select
                  placeholder="请选择"
                  style="width: 117px"
                  size="small"
                  v-model="b1t.province"
                  @change="cityChange"
                >
                  <el-option
                  v-for="item in provinces"
                  :key="item"
                  :label="item"
                  :value="item"
                  ></el-option>
                </el-select>
                <el-select
                  class="leftBT"
                  placeholder="请选择"
                  collapse-tags
                  size="small"
                  v-model="b1t.city"
                >
                  <el-option
                  v-for="item in list"
                  :key="item"
                  :label="item"
                  :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item class="PTbutton" style="width:100%">
                <el-button size="small" @click="purge()">清除</el-button>
                <el-button type="primary" size="small" @click="Search">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <!-- 下半区 -->
        <!-- tab栏切换 -->
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="全部" name="-1"></el-tab-pane>
          <el-tab-pane label="待审核" name="0"></el-tab-pane>
          <el-tab-pane label="已审核" name="1"></el-tab-pane>
          <el-tab-pane label="已拒绝" name="2"></el-tab-pane>
        </el-tabs>
        <el-alert
          :title="'数据一共' + informationList.counts + '条'"
          type="info"
          show-icon
          :closable="false"
        ></el-alert>
        <Xumiao :list="informationList.items" @choice="choice"></Xumiao>
      </el-card>
    </div>
  </div>
</template>

<script>
import { direction, difficulty, questionType } from '@/api/hmmm/constants.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
import Xumiao from './Xm.vue'
import { simpleTwo, simple, choice, simpleTags, user } from '@/api/hmmm/Xm.js'
export default {
  components: {
    Xumiao
  },
  data () {
    return {
      direction,
      difficulty,
      questionType,
      provinces: provinces(),
      citys: citys,
      list: [],
      labelPosition: 'right',
      activeName: 'first',
      b1t: {
        subjectID: '', // 科目
        catalogID: '', // 二级科目
        keyWord: '', // 关键字
        tags: '', // 标签
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: '', // 录入人
        remarks: '', // 题目标注
        shortName: '', // 企业简称
        province: '', // 省份
        city: '' // 城市
      },
      subject: [],
      directorys: [],
      labelTags: [], // 标签
      informationList: [],
      userInfor: [], // 录入人
      page: 1,
      pageSize: 5,
      chkState: '' // 表格状态
    }
  },
  created () {
    this.simple()
    this.choice()
    this.user()
  },
  methods: {
    Newadditions () {
      this.$router.push({
        name: 'questions-new'
      })
    },
    async simple () {
      const data = await simple()
      this.subject = data.data
    },
    async changeSelect (val) {
      const param = { subjectID: val }
      const { data } = await simpleTwo(param)
      this.directorys = data
      const tags = await simpleTags(param)
      this.labelTags = tags.data
    },
    async choice (pageSize, pageNum) {
      this.page = pageNum || this.page
      this.pageSize = pageSize || this.pageSize
      const param = {
        page: this.page,
        pagesize: this.pageSize
      }
      if (this.chkState) {
        param.chkState = this.chkState
      }
      const { data } = await choice(param)
      this.informationList = data
    },
    async user () {
      const { data } = await user()
      this.userInfor = data
    },
    purge () {
      this.b1t = {
        value: '', // 科目
        subjectChlidren: '', // 二级科目
        keyWord: '', // 关键字
        tags: '', // 标签
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: '', // 录入人
        remarks: '', // 题目标注
        shortName: '', // 企业简称
        province: '', // 省份
        city: '' // 城市
      }
      delete this.b1t.page
      delete this.b1t.pagesize
      delete this.b1t.chkState
    },
    cityChange (val) {
      this.list = citys(val)
    },
    // 搜索
    async Search () {
      if (this.Xmtabs(this.b1t)) {
        this.choice()
      } else {
        this.b1t.page = this.page
        this.b1t.pagesize = this.pageSize
        if (this.chkState) {
          this.b1t.chkState = this.chkState
        } else {
          delete this.b1t.chkState
        }
        const { data } = await choice(this.b1t)
        this.informationList = data
      }
    },
    async handleClick (tab, event) {
      const zls = { chkState: tab.name }
      this.page = 1
      if (zls.chkState && tab.name !== '-1') {
        this.chkState = tab.name
      } else {
        this.chkState = ''
      }
      if (this.Xmtabs(this.b1t)) {
        this.choice()
      } else {
        this.Search()
      }
    },
    Xmtabs (obj) {
      let isEmpty = true
      Object.keys(obj).forEach(e => {
        if (obj[e] != null & obj[e] !== '') {
          isEmpty = false
        }
      })
      return isEmpty
    }
  }
}
</script>

<style lang="scss" scoped>
.greenBT {
  margin-left: 93.2%;
  margin-bottom: 15px;
}
.PTbutton {
  margin-left: 110px;
}
.leftBT {
  width: 116px;
  margin-left: 1px;
}
.preface{
  margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    span {
      font-size: 12px;
      color: pink;
    }
}
</style>
